package com.st.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mysql.cj.util.StringUtils;
import com.st.common.dubboProvider.product.GetSkuNamesService;
import com.st.common.to.SkuInfoTo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.st.common.utils.PageUtils;
import com.st.common.utils.Query;

import com.st.mall.product.dao.SkuInfoDao;
import com.st.mall.product.entity.SkuInfoEntity;
import com.st.mall.product.service.SkuInfoService;


@Service("skuInfoService")
@DubboService(interfaceClass = GetSkuNamesService.class)
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService, GetSkuNamesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<SkuInfoEntity> queryWrapper = new LambdaQueryWrapper<>();

        /**
         * key:
         * catelogId: 0
         * brandId: 0
         * min: 0
         * max: 0
         */
        String key = (String) params.get("key");
        if (StringUtils.isNullOrEmpty(key)) {
            queryWrapper.and((wrapper) -> {
                wrapper.eq(SkuInfoEntity::getSkuId, key).or().like(SkuInfoEntity::getSkuName, key);
            });
        }
        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isNullOrEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)) {

            queryWrapper.eq(SkuInfoEntity::getCatalogId, catelogId);
        }
        String brandId = (String) params.get("brandId");
        if (!StringUtils.isNullOrEmpty(brandId) && !"0".equalsIgnoreCase(catelogId)) {
            queryWrapper.eq(SkuInfoEntity::getBrandId, brandId);
        }
        String min = (String) params.get("min");
        if (!StringUtils.isNullOrEmpty(min)) {
            queryWrapper.ge(SkuInfoEntity::getPrice, min);
        }
        String max = (String) params.get("max");
        if (!StringUtils.isNullOrEmpty(max)) {
            BigDecimal bigDecimal = new BigDecimal(max);

            if (bigDecimal.compareTo(new BigDecimal("0")) == 1) {
                queryWrapper.ge(SkuInfoEntity::getPrice, max);
            }
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    /**
     *  被远程调用查询skuInfo的方法
     * @param skuId skuId
     * @return 远程传输的对象
     */
    @Override
    public String getSkuInfo(Long skuId) {
        SkuInfoEntity byId = this.getById(skuId);
        SkuInfoTo skuInfoTo = new SkuInfoTo();
        if(byId!=null&&!StringUtils.isNullOrEmpty(byId.getSkuName())){
            skuInfoTo.setSkuId(byId.getSkuId());
            skuInfoTo.setSkuName(byId.getSkuName());
        }
        return skuInfoTo.getSkuName();

    }
}